﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MyMVCDemo.Models.ViewModels.Member;
using MyMVCDemo.Services;

namespace MyMVCDemo.Controllers
{

    [Authorize(Roles = "Member")]
    public class MemberController : Controller
    {

        private readonly AddressServices _addressServices;
        private readonly AccountServices _accountServices;


        public MemberController(AddressServices addressServices, AccountServices accountServices)
        {
            _addressServices = addressServices;
            _accountServices = accountServices;
        }

        public IActionResult Index()
        {
            return RedirectToAction("List");
        }

        /// <summary>
        /// 当前用户的通讯录列表
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> List()
        {
            var model = await _addressServices.GetListByHostId(_accountServices.MemberQq);
            return View(model);
        }
        [HttpPost]
        public async Task<IActionResult> Search(SearchModel model)
        {
            var ans = await _addressServices.SearchListByHostId(_accountServices.MemberQq, model.SearchTxt);
            //此处_accountServices里注入的MemberQq，是从cookies中的claim里获取的。
            return View("List", ans);
        }

        /// <summary>
        /// 创建新通讯录记录
        /// </summary>
        /// <returns></returns>
        public IActionResult Create()
        {
            return View();
        }
        [HttpPost]


        public async Task<IActionResult> Create(AddressCreateModel model)
        {
            //如果校验不通过，返回原页面，带着输入值
            if (!ModelState.IsValid)
                return View(model);
            try
            {
                //存盘
                await _addressServices.Create(model);
                return RedirectToAction(actionName: "List");
            }
            catch (Exception e)
            {
                ModelState.AddModelError(key: "", e.Message);
                return View(model);
            }
        }


        /// <summary>
        /// 详细页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> Detail(Guid id)
        {
            var model = await _addressServices.Detail(id);
            if (model == null)
                return RedirectToAction("List");
            return View(model);
        }


        /// <summary>
        /// 编辑录记录
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Edit(Guid id)
        {
            var model = await _addressServices.GeteEditModel(id);
            if (model == null)
                return RedirectToAction("List");
            return View(model);
        }
        [HttpPost]


        public async Task<IActionResult> Edit(AddressEditModel model)
        {
            //如果校验不通过，返回原页面，带着输入值
            if (!ModelState.IsValid)
                return View(model);

            try
            {
                //存盘
                await _addressServices.Edit(model);
                return RedirectToAction(actionName: "Detail", new { id = model.Id });
            }
            catch (Exception e)
            {
                ModelState.AddModelError(key: "", e.Message);
                return View(model);
            }
        }

        public async Task<IActionResult> Delete(Guid id)
        {
            await _addressServices.Delete(id);
            return RedirectToAction(actionName: "List");
        }
        /// <summary>
        /// 复杂删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete2([FromForm] Guid id)//从form中取出id
        {
            await _addressServices.Delete(id);
            return RedirectToAction(actionName: "List");
        }



    }
}
